{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c4f39e6f-5e3d-4bab-b97f-f071ecbffb4f",
   "metadata": {},
   "source": [
    "# 电机修改ID"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb5fb6d5-cd45-46d1-9b94-99d0a49dd07a",
   "metadata": {},
   "source": [
    "## 查询总线设备列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "5fef218b-cbff-47a7-8a1d-f496b859e663",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "device_id: 0x10, type: robodyno Pro 12\n"
     ]
    }
   ],
   "source": [
    "from robodyno import Robot\n",
    "from robodyno.nodes import MotorFactory\n",
    "robot = Robot()\n",
    "type_dict = {\n",
    "    '0': 'robodyno Pro 44',\n",
    "    '1': 'robodyno Pro 12',\n",
    "    '97': 'robodyno nano_board',\n",
    "    '98': 'robodyno stepper_board'\n",
    "}\n",
    "for id in range(0x01, 0x3F):\n",
    "    motor = robot.add_device('motor0', MotorFactory(), id)\n",
    "    info = motor.get_api_ver(0.1)\n",
    "    if info:\n",
    "        print('device_id: 0x{:02X}, type: {}'.format(id, type_dict.get(str(info['device_uuid']))))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3dc10d07-9b1d-4938-bcf2-5b2034100d62",
   "metadata": {},
   "source": [
    "## 设置新id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "1a100e50-1977-4e6f-be72-e0bfa4f297ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "prev_id = 0x10\n",
    "new_id = 0x11\n",
    "from protobot.can_bus import Robot\n",
    "from protobot.can_bus.nodes import MotorFactory\n",
    "robot = Robot()\n",
    "motor = robot.add_device('motor0', MotorFactory(), prev_id)\n",
    "motor.set_can_id(id = new_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6ebc186-30ad-4c50-8421-2b354d3b9f84",
   "metadata": {},
   "source": [
    "## 保存设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "57ec80d0-1a3d-4953-b04d-ee439a938184",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'device_uuid': 1, 'main_version': 0, 'sub_version': 3}\n"
     ]
    }
   ],
   "source": [
    "motor = robot.add_device('new_motor', MotorFactory(), new_id)\n",
    "motor.save_configuration()\n",
    "print(motor.get_api_ver(0.5))\n",
    "motor.reboot()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
